package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.*;
import com.eastfair.imaster.entity.Collection;
import com.eastfair.imaster.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 收藏表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/collection")
@Api(value = "Collection", tags = "收藏表")
public class CollectionController extends SuperController<CollectionService, Long, Collection, CollectionPageQuery, CollectionSaveDTO, CollectionUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Collection> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Collection> collectionList = list.stream().map((map) -> {
            Collection collection = Collection.builder().build();
            //TODO 请在这里完成转换
            return collection;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(collectionList));
    }

    /**
     * 收藏
     * 未收藏时添加收藏记录，已收藏时取消收藏
     * @param collectionDTO subjectType、subjectId
     * @return 成功true 失败false
     */
    @ApiOperation(value = "收藏/取消收藏",notes = "收藏/取消收藏")
    @PostMapping("/collect")
    public R<Boolean> collect(@RequestBody @Validated CollectionDTO collectionDTO){
        this.baseService.saveCollection(collectionDTO);
        return success();
    }

    /**
     * 校验是否收藏
     * @param query subjectType、subjectId
     * @return 已收藏ture/未收藏false
     */
    @PostMapping("/check")
    public R<Boolean> checkCollect(@RequestBody CollectionQuery query){
        if (query.getSubjectType() == null || query.getSubjectId() == null) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        return success(this.baseService.check(query.getSubjectType(),query.getSubjectId()) != null);
    }

    /**
     * 校验有没有收藏过 并返回收藏数
     * @param query type、id
     * @return collectionDTO{isCollect,num}
     */
    @PostMapping("/checkWithNum")
    public R<CollectionDTO> checkWithNum(@RequestBody CollectionQuery query) {
        if (query.getSubjectType() == null || query.getSubjectId() == null) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        return success(this.baseService.checkWithNum(query.getSubjectType(),query.getSubjectId()));
    }

    /**
     * 批量校验是否收藏
     * @param query subjectType、subjectIds
     * @return 已收藏的内容ID集合 此处用set 因为组装数据频繁调用contains
     */
    @PostMapping("/checkBatch")
    public R<Set<Long>> checkCollectBatch(@RequestBody CollectionQuery query){
        if (query.getSubjectType() == null || query.getSubjectIds() == null || query.getSubjectIds().isEmpty()) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        return success(this.baseService.checkBatch(query.getSubjectType(),query.getSubjectIds()));
    }

    /**
     * 批量校验是否收藏过 并返回收藏数
     * @param query type、id
     * @return key-subjectId value-collectionDTO{isCollect,num}
     */
    @PostMapping("/checkBatchWithNum")
    public R<Map<Long,CollectionDTO>> checkBatchWithNum(@RequestBody CollectionQuery query){
        if (query.getSubjectType() == null || query.getSubjectIds() == null || query.getSubjectIds().isEmpty()) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        return success(this.baseService.checkBatchWithNum(query.getSubjectType(),query.getSubjectIds()));
    }
}
